﻿using System.Collections.Generic;
using NBpn.Exception;

namespace NBpn.Network
{
    public class Net : INet
    {
        private Neuron neuron;

        internal Net()
        {
        }

        internal Net(Neuron neuron) : this()
        {
            this.neuron = neuron;
        }

        internal Net(Neuron neuron, double value)
            : this(neuron)
        {
            this.neuron.InputConnections.Add(new Connection(value));
        }

        #region INet Members

        public IList<Connection> Connections
        {
            get { return this.neuron.InputConnections; }
        }

        public double Value
        {
            get
            {
                if (neuron.InputConnections.Count == 0)
                    throw new BpnException(Messages.TheNeuronIsNotConnected);

                if (neuron.InputConnections[0].To == null && neuron.InputConnections.Count == 0)
                    throw new BpnException(Messages.TheNeuronIsNotConnected);

                double Total = 0;

                if (neuron.InputConnections.Count == 1 && neuron.InputConnections[0].To.IsInputNeuron)
                {
                    return neuron.InputConnections[0].Weight;
                }

                foreach (Connection connection in neuron.InputConnections)
                {
                    Total += connection.From.Output*connection.Weight;
                }

                return Total;
            }
        }

        #endregion
    }
}